import requests
import json

search_word = input("请输入要查询的词：")

url = "https://api.mojidict.com/parse/functions/word-clickSearchV2"

headers = {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "content-type": "text/plain",
    "origin": "https://www.mojidict.com",
    "referer": "https://www.mojidict.com/",
    "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Microsoft Edge";v="138"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"',
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0"
}

data = f'''{{
    "searchText": "{search_word}",
    "langEnv": "zh-CN_ja",
    "_ClientVersion": "js4.3.1",
    "_ApplicationId": "E62VyFVLMiW7kvbtVq3p",
    "g_os": "PCWeb",
    "g_ver": "v4.10.0.20250610",
    "_InstallationId": "b8d46802-914e-49c5-b900-a71452b31f7e"
}}'''

response = requests.post(url, headers=headers, data=data)

if response.status_code == 200:
    json_data = response.json()

    # 打印原始数据结构
    # print("\n🧾 原始数据结构预览：")
    # print(json.dumps(json_data, indent=2, ensure_ascii=False))

    try:
        result = json_data["result"]["result"]

        word_info = result["word"][0]
        spell = word_info.get("spell", "")
        romaji = word_info.get("romaji", "")
        excerpt = word_info.get("excerpt", "")
        etym = word_info.get("etymology", "")
        
        subdetails = result.get("subdetails", [])
        meanings = [item["title"] for item in subdetails]

        print("\n📘 查询结果：")
        print(f"原词：{search_word}")
        print(f"假名：{spell}")
        print(f"罗马音：{romaji}")
        print(f"词源：{etym}")
        print("释义：")
        for i, m in enumerate(meanings, 1):
            print(f"  {i}. {m}")
        print(f"简要注释：{excerpt}")

    except Exception as e:
        print("⚠️ 无法解析结果：", e)


else:
    print(f"❌ 请求失败，状态码：{response.status_code}")
    print(response.text)
